1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 import java.util.concurrent.ArrayBlockingQueue;
42 import java.util.concurrent.ConcurrentHashMap;
43 import java.util.concurrent.ConcurrentLinkedDeque;
44 import java.util.concurrent.ConcurrentLinkedQueue;
45 import java.util.concurrent.LinkedBlockingDeque;
46 import java.util.concurrent.LinkedBlockingQueue;
47 import java.util.concurrent.LinkedTransferQueue;
48 import java.util.concurrent.PriorityBlockingQueue;
49 import java.util.LinkedList;
50 import java.util.PriorityQueue;
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.Collections;
54 import java.util.List;
55 import java.util.Queue;
56 import java.util.Map;
57
58 public class GCRetention {
59
60 int count = 1024 * 1024;
61
62 final Map<String,String> results = new ConcurrentHashMap<String,String>();
63
64 Collection<Queue<Boolean>> queues() {
65 List<Queue<Boolean>> queues = new ArrayList<Queue<Boolean>>();
66 queues.add(new ConcurrentLinkedDeque<Boolean>());
67 queues.add(new ConcurrentLinkedQueue<Boolean>());
68 queues.add(new ArrayBlockingQueue<Boolean>(count, false));
69 queues.add(new ArrayBlockingQueue<Boolean>(count, true));
70 queues.add(new LinkedBlockingQueue<Boolean>());
71 queues.add(new LinkedBlockingDeque<Boolean>());
72 queues.add(new PriorityBlockingQueue<Boolean>());
73 queues.add(new PriorityQueue<Boolean>());
74 queues.add(new LinkedList<Boolean>());
75 queues.add(new LinkedTransferQueue<Boolean>());
76
77
78
79
80
81
82 Collections.shuffle(queues);
83 return queues;
84 }
85
86 void prettyPrintResults() {
87 List<String> classNames = new ArrayList<String>(results.keySet());
88 Collections.sort(classNames);
89 int maxClassNameLength = 0;
90 int maxNanosLength = 0;
91 for (String name : classNames) {
92 if (maxClassNameLength < name.length())
93 maxClassNameLength = name.length();
94 if (maxNanosLength < results.get(name).length())
95 maxNanosLength = results.get(name).length();
96 }
97 String format = String.format("%%%ds %%%ds nanos/item%%n",
98 maxClassNameLength, maxNanosLength);
99 for (String name : classNames)
100 System.out.printf(format, name, results.get(name));
101 }
102
103 void test(String[] args) {
104 if (args.length > 0)
105 count = Integer.valueOf(args[0]);
106
107 for (Queue<Boolean> queue : queues())
108 test(queue);
109 results.clear();
110 for (Queue<Boolean> queue : queues())
111 test(queue);
112
113 prettyPrintResults();
114 }
115
116 void test(Queue<Boolean> q) {
117 long t0 = System.nanoTime();
118 for (int i = 0; i < count; i++)
119 check(q.add(Boolean.TRUE));
120 System.gc();
121 System.gc();
122 Boolean x;
123 while ((x = q.poll()) != null)
124 equal(x, Boolean.TRUE);
125 check(q.isEmpty());
126
127 for (int i = 0; i < 10 * count; i++) {
128 for (int k = 0; k < 3; k++)
129 check(q.add(Boolean.TRUE));
130 for (int k = 0; k < 3; k++)
131 if (q.poll() != Boolean.TRUE)
132 fail();
133 }
134 check(q.isEmpty());
135
136 String className = q.getClass().getSimpleName();
137 long elapsed = System.nanoTime() - t0;
138 int nanos = (int) ((double) elapsed / (10 * 3 * count));
139 results.put(className, String.valueOf(nanos));
140 }
141
142
143 volatile int passed = 0, failed = 0;
144 void pass() {passed++;}
145 void fail() {failed++; Thread.dumpStack();}
146 void fail(String msg) {System.err.println(msg); fail();}
147 void unexpected(Throwable t) {failed++; t.printStackTrace();}
148 void check(boolean cond) {if (cond) pass(); else fail();}
149 void equal(Object x, Object y) {
150 if (x == null ? y == null : x.equals(y)) pass();
151 else fail(x + " not equal to " + y);}
152 public static void main(String[] args) throws Throwable {
153 new GCRetention().instanceMain(args);}
154 public void instanceMain(String[] args) throws Throwable {
155 try {test(args);} catch (Throwable t) {unexpected(t);}
156 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
157 if (failed > 0) throw new AssertionError("Some tests failed");}
158 }